//
//  QLCircleOfFriendsCell.m
//  神约
//
//  Created by qlwl on 16/12/14.
//  Copyright © 2016年 qlwl. All rights reserved.
//

#import "QLCircleOfFriendsCell.h"
#import "UIView+FrameKIt.h" 
#import "JRMenuView.h"

@interface QLCircleOfFriendsCell ()<JRMenuDelegate>
{
    JRMenuView * jrMenu;
}
@end

@implementation QLCircleOfFriendsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _userHead = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
        _userHead.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:_userHead];
        _nameLab = [[UILabel alloc] initWithFrame:CGRectZero];
        _nameLab.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_nameLab];
        _sexIma = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
        _sexIma.contentMode = UIViewContentModeCenter;
        [self.contentView addSubview:_sexIma];
        _contentLab = [[UILabel alloc] initWithFrame:CGRectZero];
        _contentLab.font = [UIFont systemFontOfSize:14];
        _contentLab.numberOfLines = 0;
        [self.contentView addSubview:_contentLab];
        _ImaBgView = [[QLCicleOfFriendsImaView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_ImaBgView];
        _timeLab = [[UILabel alloc] initWithFrame:CGRectZero];
        _timeLab.textColor = [UIColor lightGrayColor];
        _timeLab.font = [UIFont systemFontOfSize:11];
        [self.contentView addSubview:_timeLab];
        _addressLab = [[TYAttributedLabel alloc] initWithFrame:CGRectZero];
        _addressLab.numberOfLines = 1;
        [self.contentView addSubview:_addressLab];
        
//        _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_commentBtn setImage:[UIImage imageNamed:@"commentInputBtn"] forState:UIControlStateNormal];
//        [_commentBtn addTarget:self action:@selector(commentBtnClicks) forControlEvents:UIControlEventTouchUpInside];
//        [self.contentView addSubview:_commentBtn];
        
        _priseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_priseBtn setImage:[UIImage imageNamed:@"commentInputBtn"] forState:UIControlStateNormal];
        [_priseBtn addTarget:self action:@selector(priseBtnBtnClicks) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_priseBtn];
        
//        _priseLab = [[UILabel alloc] initWithFrame:CGRectZero];
//        _priseLab.font = [UIFont systemFontOfSize:12];
//        _priseLab.textColor = [UIColor lightGrayColor];
//        [self.contentView addSubview:_priseLab];
        
        _commentView = [[QLCicleOfFriendCommentView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_commentView];
        
    }
    return self;
}

- (void)setCommentItemClick:(void (^)(NSIndexPath *, float))commentItemClick
{
    _commentItemClick = commentItemClick;
    _commentView.commentItemClick = [_commentItemClick copy];
}

- (void)refreshHeight
{
    float transverseSpace = 8;
    float verticalSpace = 6;
    
    _userHead.f_left = transverseSpace;
    _userHead.f_top = transverseSpace;
    
    _nameLab.f_top = _userHead.f_top;
    _nameLab.f_left = _userHead.f_right + transverseSpace;
    [_nameLab sizeToFit];
    
    _sexIma.f_left = _nameLab.f_right + verticalSpace;
    _sexIma.f_centerY = _nameLab.f_centerY;
    
    _contentLab.f_width = iphoneWidth - _nameLab.f_left  - 8;
    [_contentLab sizeToFit];
    _contentLab.f_left = _nameLab.f_left;
    if (_contentLab.f_height) {
        _contentLab.f_top = _nameLab.f_bottom + verticalSpace;
    }else{
        _contentLab.f_top = _nameLab.f_bottom ;
    }
    //非必有的
    _ImaBgView.palyVideo = [self.palyVideo copy];
    _ImaBgView.showPhoto = [self.showPhoto copy];
    if (_ImaBgView.f_height) {
        _ImaBgView.f_left = _nameLab.f_left;
        _ImaBgView.f_top = _contentLab.f_bottom + verticalSpace;
        _ImaBgView.hidden = NO;
    }else{
        _ImaBgView.f_left = _nameLab.f_left;
        _ImaBgView.f_top = _contentLab.f_bottom;
        _ImaBgView.f_height = 0;
        _ImaBgView.hidden = YES;
    }
    // 非必有的
    [_addressLab setFrameWithOrign:CGPointMake(0, 0) Width:iphoneWidth - _nameLab.f_left  - 8];
    if (_addressLab.f_height) {
        _addressLab.f_left = _nameLab.f_left;
        _addressLab.f_top = _ImaBgView.f_bottom + verticalSpace;
        _addressLab.hidden = NO;
    }else{
        _addressLab.f_left = _nameLab.f_left;
        _addressLab.f_top = _ImaBgView.f_bottom ;
        _addressLab.f_height = 0;
        _addressLab.hidden = YES;
    }
    // 时间是必有的
    [_timeLab sizeToFit];
    _timeLab.f_left = _nameLab.f_left;
    _timeLab.f_top = _addressLab.f_bottom + 6;

//    [_priseLab sizeToFit];
//    _priseLab.f_right = iphoneWidth - transverseSpace;
//    _priseLab.f_centerY = _timeLab.f_centerY;
    
    [_priseBtn sizeToFit];
    _priseBtn.f_height = 38;
    _priseBtn.f_width = 30;
    _priseBtn.f_right = iphoneWidth - transverseSpace;
    _priseBtn.f_centerY = _timeLab.f_centerY;
    
    
    [_commentBtn sizeToFit];
    _commentBtn.f_right = _priseBtn.f_left - 8;
    _commentBtn.f_centerY = _timeLab.f_centerY;
    
    
    [_commentView refreshHeightWithWidth:iphoneWidth - _nameLab.f_left  - transverseSpace];
    if (_commentView.f_height) {
        _commentView.f_top = _timeLab.f_bottom + 6;
        _commentView.f_left = _nameLab.f_left;
        _commentView.hidden = NO;
    }else{
        _commentView.f_top = _timeLab.f_bottom ;
        _commentView.f_left = _nameLab.f_left;
        _commentView.hidden = YES;
    }
    self.f_height = _commentView.f_bottom + 8 + 4;
}
- (void)commentBtnClicks
{
//    if (self.commentBtnClick) {
//        self.commentBtnClick(self);
//    }
}
- (void)priseBtnBtnClicks
{
//    if (self.sendPrise) {
//        self.sendPrise();
//    }
    [self showJRMenu:self.priseBtn];
}

- (void)showJRMenu:(UIButton *)sender
{
    
    
    
    if (!jrMenu) {
        jrMenu = [[JRMenuView alloc] init];
    }
    [jrMenu setTargetView:sender InView:self.contentView];
    if (self.CLICKMENUBLOCK) {
        NSArray * itemsArray = self.CLICKMENUBLOCK();
        [jrMenu setTitleArray:itemsArray];
    }
    jrMenu.delegate = self;
    [self.contentView addSubview:jrMenu];
    [jrMenu show];
}

#pragma mark - JRMenuDelegate
-(void)hasSelectedJRMenuIndex:(NSInteger)jrMenuIndex
{
    if (self.CLICKITEMSBLOCK) {
        self.CLICKITEMSBLOCK(jrMenuIndex);
    }
}

@end
